package com.cake.base.interceptor;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.cake.base.config.Constant;
import com.cake.entity.XxAdmin;

@Component
public class LoginInterceptor implements HandlerInterceptor {

	
//	private static final Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);
	
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception e)
			throws Exception {
		
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView modelAndView)
			throws Exception {
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
		if(request.getSession().getAttribute(Constant.USER_SESSION_KEY) == null){
			response.setCharacterEncoding("UTF-8");
		    response.setContentType("application/json; charset=utf-8"); 
			PrintWriter out = response.getWriter();  
	        out.print("{\"code\":\"999999\", \"msg\":\"用户登录失效\"}");
	        return false;
		}
		return true;
	}
}
